#include "bsp_magnetic.h"
#include "bsp_param.h"
#include "bsp_timer.h"
// #include "esp_adc/adc_oneshot.h"
static uint32_t vib_ticks = 0;
static uint32_t magnetic_ticks = 0;
static uint32_t vib_period = VIB_0HZ_TICKS;
/* 磁振频率参数 */
static const uint32_t vib_period_ticks[] = { VIB_0HZ_TICKS, VIB_2HZ_TICKS, VIB_5HZ_TICKS, VIB_10HZ_TICKS };


/* 磁场H桥方向调节 */
static inline void magnetic_bridge_a_to_b(void)
{
    gpio_set_level(GPIO_BRIDGE_CTRL_1, 0);
    gpio_set_level(GPIO_BRIDGE_CTRL_0, 1);
}

static inline void magnetic_bridge_b_to_a(void)
{
    gpio_set_level(GPIO_BRIDGE_CTRL_0, 0);
    gpio_set_level(GPIO_BRIDGE_CTRL_1, 1);
}

static inline void magnetic_bridge_stop(void)
{
    gpio_set_level(GPIO_BRIDGE_CTRL_0, 0);
    gpio_set_level(GPIO_BRIDGE_CTRL_1, 0);
}

static inline void magnetic_vib_output_stop(void)
{
    gpio_set_level(GPIO_VIB_OUTPUT_ENABLE, 0);
}


// adc_oneshot_unit_handle_t adc1_handle;
// adc_oneshot_unit_init_cfg_t init_config1 = {
//     .unit_id = ADC_UNIT_1,
// };
// //-------------ADC1 Config---------------//
// adc_oneshot_chan_cfg_t config = {
//     .bitwidth = ADC_BITWIDTH_12,
//     .atten = ADC_ATTEN_DB_11,
// };
// static void magnetic_adc_init()
// {

//     adc_oneshot_new_unit(&init_config1, &adc1_handle);
//     adc_oneshot_config_channel(adc1_handle, CL_BREAK_CHANNEL, &config);
// }

// void magnetic_break_read()
// {
//     int adc_num = 0;
//     adc_oneshot_read(adc1_handle, CL_BREAK_CHANNEL, &adc_num);
//     printf("\r\nmagnetic_adc=%d\r\n", adc_num);
//     // return adc_num;
// }





void magnetic_init()
{
    gpio_config_t io_conf = {};
    //disable interrupt
    io_conf.intr_type = GPIO_INTR_DISABLE;
    //set as output mode
    io_conf.mode = GPIO_MODE_OUTPUT;
    //bit mask of the pins that you want to set,e.g.GPIO18/19
    io_conf.pin_bit_mask = GPIO_MAGANETIC_PIN_SEL;
    //disable pull-down mode
    io_conf.pull_down_en = 0;
    //disable pull-up mode
    io_conf.pull_up_en = 0;
    //configure GPIO with the given settings
    gpio_config(&io_conf);
    gpio_set_level(GPIO_MAGANETIC_POWER_ENABLE, 0);
    gpio_set_level(GPIO_BRIDGE_CTRL_0, 0);
    gpio_set_level(GPIO_BRIDGE_CTRL_1, 0);
    gpio_set_level(GPIO_MAGANETIC_VOLTAGE_CTRL_0, 0);
    gpio_set_level(GPIO_MAGANETIC_VOLTAGE_CTRL_1, 0);
    /* vib */
    gpio_config_t io_conf2 = {};
    //disable interrupt
    io_conf2.intr_type = GPIO_INTR_DISABLE;
    //set as output mode
    io_conf2.mode = GPIO_MODE_OUTPUT;
    //bit mask of the pins that you want to set,e.g.GPIO18/19
    io_conf2.pin_bit_mask = GPIO_VIB_PIN_SEL;
    //disable pull-down mode
    io_conf2.pull_down_en = 0;
    //disable pull-up mode
    io_conf2.pull_up_en = 0;
    //configure GPIO with the given settings
    gpio_config(&io_conf2);
    gpio_set_level(GPIO_VIB_POWER_ENABLE, 0);
    gpio_set_level(GPIO_VIB_OUTPUT_ENABLE, 0);
    gpio_set_level(GPIO_VIB_VOLTAGE_CTRL_0, 0);
    gpio_set_level(GPIO_VIB_VOLTAGE_CTRL_1, 0);

    // magnetic_adc_init();
}

static uint8_t magnetic_ccqd = POWER_MAX;
static uint8_t magnetic_czfd = POWER_MAX;
static uint8_t magnetic_czpl = POWER_MAX;
void magnetic_param_change(uint16_t *magnetic_param)
{
    if(magnetic_param[MODE_CCQD] != magnetic_ccqd){
        switch(magnetic_param[MODE_CCQD]){
            case POWER_0:
                gpio_set_level(GPIO_MAGANETIC_POWER_ENABLE, 0);
                break;
            case POWER_1:
                gpio_set_level(GPIO_MAGANETIC_VOLTAGE_CTRL_0, 0);
                gpio_set_level(GPIO_MAGANETIC_VOLTAGE_CTRL_1, 0);
                gpio_set_level(GPIO_MAGANETIC_POWER_ENABLE, 1);
                break;
            case POWER_2:
                gpio_set_level(GPIO_MAGANETIC_VOLTAGE_CTRL_0, 1);
                gpio_set_level(GPIO_MAGANETIC_VOLTAGE_CTRL_1, 0);
                gpio_set_level(GPIO_MAGANETIC_POWER_ENABLE, 1);
                break;
        
            case POWER_3:
                gpio_set_level(GPIO_MAGANETIC_VOLTAGE_CTRL_1, 1);
                gpio_set_level(GPIO_MAGANETIC_POWER_ENABLE, 1);
                break;
            default:
                break;
        
        }
        magnetic_ccqd = magnetic_param[MODE_CCQD];
    }

    if(magnetic_param[MODE_CZFD] != magnetic_czfd){
        switch(magnetic_param[MODE_CZFD]){
            case POWER_0:
                gpio_set_level(GPIO_VIB_POWER_ENABLE, 0);
                break;
            case POWER_1:
                gpio_set_level(GPIO_VIB_VOLTAGE_CTRL_0, 0);
                gpio_set_level(GPIO_VIB_VOLTAGE_CTRL_1, 0);
                gpio_set_level(GPIO_VIB_POWER_ENABLE, 1);
                break;
            case POWER_2:
                gpio_set_level(GPIO_VIB_VOLTAGE_CTRL_0, 1);
                gpio_set_level(GPIO_VIB_VOLTAGE_CTRL_1, 0);
                gpio_set_level(GPIO_VIB_POWER_ENABLE, 1);
                break;
    
            case POWER_3:
                gpio_set_level(GPIO_VIB_VOLTAGE_CTRL_1, 1);
                gpio_set_level(GPIO_VIB_POWER_ENABLE, 1);
                break;
            default:
                break;
    
        }
        magnetic_czfd = magnetic_param[MODE_CZFD];
    }

    if(magnetic_param[MODE_CZPL] != magnetic_czpl){
        switch(magnetic_param[MODE_CZPL]){
            case POWER_0:
                vib_period = vib_period_ticks[POWER_0];
                break;
            case POWER_1:
                vib_period = vib_period_ticks[POWER_1];
                break;
            case POWER_2:
                vib_period = vib_period_ticks[POWER_2];
                break;
            case POWER_3:
                vib_period = vib_period_ticks[POWER_3];
                break;
            default:
                break;
    
        }
        magnetic_czpl = magnetic_param[MODE_CZPL];
    }


}

void magnetic_tick_service()
{
    sys_main_param_t* timer2_sys_param = NULL;
    timer2_sys_param = Get_sys_param_pos();
    if(timer2_sys_param->sys_status != SYS_RUN_NORMAL){
        magnetic_ticks = 0;
        vib_ticks = 0;
        magnetic_bridge_stop();
        magnetic_vib_output_stop();
        return;
    }

    /* 磁场强度 */
    if(magnetic_ccqd == POWER_0){
        magnetic_ticks = 0;
    }else{
        if((++magnetic_ticks) >= MAGANETIC_TICKS_PERIOD) 
            magnetic_ticks = 0;
        if(magnetic_ticks == 0)
            magnetic_bridge_a_to_b();
        if(magnetic_ticks == MAGNANETIC_OUTPUT_TICKS)
            magnetic_bridge_stop();
        if(magnetic_ticks == (MAGANETIC_TICKS_PERIOD/2))
            magnetic_bridge_b_to_a();
        if(magnetic_ticks == (MAGANETIC_TICKS_PERIOD/2 + MAGNANETIC_OUTPUT_TICKS))
            magnetic_bridge_stop();
    }

    /* 磁振频率 */
    if(magnetic_czpl == POWER_0){
        vib_ticks = 0;
    }else{
        if((++vib_ticks) >= vib_period) 
            vib_ticks = 0;
        if(vib_ticks == 0)
            gpio_set_level(GPIO_VIB_OUTPUT_ENABLE, 1);
        if(vib_ticks == VIB_OUTPUT_TICKS)
            gpio_set_level(GPIO_VIB_OUTPUT_ENABLE, 0);
    }
}











